package joindemo.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import joindemo.common.constants.CommonRedisKeyConstant;
import joindemo.domain.po.Message;
import joindemo.mapper.MessageMapper;
import joindemo.service.IMessageService;
import joindemo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageService extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public Boolean deleteMessage(Long messageId){
        log.info("开始执行消息删除服务，参数：{}",messageId);
        return removeById(messageId);
    }

    @Override
    public Long getUnreadCount(){
        long userId= StpUtil.getLoginIdAsLong();
        if(redisUtil.hasKey(CommonRedisKeyConstant.UNREAD_MESSAGE_COUNT+userId)) {
            return (Long) redisUtil.get(CommonRedisKeyConstant.UNREAD_MESSAGE_COUNT + userId);
        }
        return 0L;
    }
}
